/** 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package it.free.i.virtual.machine.hal.datarendering.impl;

import it.free.i.CommonFunctions;
import it.free.i.virtual.machine.hal.datarendering.BeanInjector;

import java.util.Properties;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

/**  
 * Spring object injection implementation. This is hidden by the object injector factory and by the ObjectInjector
 * interface
 * @author stefano
 */
public class BeanInjectorSpringImpl implements BeanInjector {

    private static BeanInjectorSpringImpl OBJECT_INJECTOR_SPRING_IMPL = null;
    private BeanFactory factory = null;
    private static Logger logger = null;
    
    private static final String SPRING_CONFIG_FILE_NAME = "beans.xml";
    
    static{
           logger = Logger.getLogger(BeanInjectorSpringImpl.class);
           getInstance().setup();
    }
    
    /**
     * Hidden constructor, goes through the factory
     */
    private BeanInjectorSpringImpl(){}
    
    /**
     * Singleton usual accessor
     * @return the unique instance kept by the singleton
     */
    public static final BeanInjectorSpringImpl getInstance(){
        
        if(OBJECT_INJECTOR_SPRING_IMPL == null){
           synchronized(BeanInjectorSpringImpl.class){
               if(OBJECT_INJECTOR_SPRING_IMPL == null){
                   OBJECT_INJECTOR_SPRING_IMPL = new BeanInjectorSpringImpl();
               }
           }
        }
        return OBJECT_INJECTOR_SPRING_IMPL;
    }
    
    /**
     * Do practical actions for this implementation specific needs
     */
    private void setup(){
        
        logger.info("Initializing spring implemenntation object injector");
        String fullClasspathFilePath = CommonFunctions.getConfigFileFromClasspath(SPRING_CONFIG_FILE_NAME);
        
        if(fullClasspathFilePath == null){
           logger.error("Unable to locate in classpath resource [" + SPRING_CONFIG_FILE_NAME + "], unable to proceed, please ensure that file is actually in classpath");
           return;
        }
        
        logger.info("Using beans definition in: [" + fullClasspathFilePath + "]");
        factory = new XmlBeanFactory(new FileSystemResource(fullClasspathFilePath));
    }
    
    public Object getBeanById(String beanName) throws Exception {
        
        Object bean = this.factory.getBean(beanName);
        return bean;
    }

    public void setup(Properties configuration) throws Exception {
        logger.info("Setup by properties not configured, looking for default configuration file: " + SPRING_CONFIG_FILE_NAME);
    }

}
